#ifndef _MIX_THREAD_HELPER_H_
#define _MIX_THREAD_HELPER_H_

#if defined( _WIN32) || defined( _WIN64)
#define M_WIN
#include <windows.h>
#include <time.h>
#else
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>		// for ENOMEM,EDEADLK,etc...
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <string>

#include "../include/typedef.h"

#ifndef MIX_LOG
#define LOG_CRITICAL 
#ifdef M_WIN
#define MIX_LOG( priority, format, ...)
#else
#define MIX_LOG( priority, format, args...)
#endif
#endif

namespace mix
{

template< typename T>
void safeDelete( T*& p) {
    if ( p) {
        delete p;
        p = NULL;
    }
}

template< typename T>
void safeDeleteArray( T*& p) {
    if ( p) {
        delete []p;
        p = NULL;
    }
}

#ifndef M_WIN
inline bool futureAbsTime( long msec, struct timespec& ts) {
    struct timeval tv;
    if ( gettimeofday( &tv, NULL))
        return false;
    ts.tv_sec = tv.tv_sec + msec / 1000;
    ts.tv_nsec = tv.tv_usec * 1000 + msec % 1000 * 1000 * 1000;
    return true;
}
#endif

}   // namespace mix

#endif  // _MIX_THREAD_HELPER_H_
